{$MODE objfpc}

program TestBed;
uses 
  SimplePython, Python, PythonTypes, PythonObject, PythonTupleObject, 
  PythonAbstract, PythonIntObject, PythonStringObject,
  Emb;

var
	//pName, pFunc, pPathObj: PyObjectPtr;
	pInt, pStr, pOther: TPythonObject;
	pathObject: TPythonObject;
	helloModule, sysModule, embModule: TPythonModule;
	res: TPythonObject;
	helloFunc: TPythonFunction;
	insertMethod: TPythonFunction;
	pArgs: TPythonTuple;
  py: TPythonEngine;
begin
	WriteLn('Python Testbed');
	
	//Load Python
	//Py_Initialize();
	TPythonEngine.Init();
	py := TPythonEngine.Instance;

	//PyRun_SimpleStringFlags('import os, sys', nil);
	//PyRun_SimpleStringFlags('sys.path.insert(0, "")', nil);	
	
	sysModule := TPythonModule.LoadModule('sys');
	pathObject := sysModule.GetAttr('path');
	insertMethod := pathObject.GetAttr('insert').AsFunction();
  WriteLn('InsertMethod ', HexStr(insertMethod));
  
	pInt := TPythonObject.FromInt(0);
	WriteLn(1, ' - ', pInt.ToString());
	pStr := TPythonObject.FromString('');
	WriteLn(2, ' - ', pStr.ReadString());
  	
  pArgs := TPythonTuple.Create(2);
  pArgs.SetItem(0, pInt);
  pArgs.SetItem(1, pStr);
  WriteLn('Args ', pArgs.ToString());
  
  res := insertMethod.Call(pArgs);
	WriteLn(res.ToString());
	res.Free();
  pArgs.Free();

  //
  // Extensions
  //
  RegisterPythonExtension();
  
  WriteLn('------------------');
  
  embModule := TPythonModule.LoadModule('emb');
  embModule.CallProcedure('myhello');
  embModule.Free();
  
  py.RunSimple('import emb');	  
  py.RunSimple('emb.myhello()');	
  py.RunSimple('print "Testing - ", emb.myhello()');	
  WriteLn('------------------');
  
  pOther := TPythonObject.FromString('Hello World');
  pArgs := TPythonTuple.Create(1);
  
  WriteLn('pOther ref cnt: ', pOther.ReferenceCount);
  WriteLn('pOther added to pArgs');
  pArgs.SetItem(0, pOther);
  
	WriteLn('pArgs -> ', pArgs.ReferenceCount);
  WriteLn('pOther ref cnt: ', pOther.ReferenceCount);
  pArgs.Free();
  WriteLn('pArgs -> is Freed ');
  WriteLn('pOther ref cnt: ', pOther.ReferenceCount);
	
	
	WriteLn('Path: ', pathObject.ToString());	
	
	helloModule := TPythonModule.LoadModule('hello');
	helloFunc := helloModule.GetAttr('hello').AsFunction();
	res := helloFunc.Call();
	res.Free();	
	
	WriteLn('Python loaded');
	
	py.RunSimple('print "Hello World! Python!"');
	py.RunSimple('def myHello(): '#10'   print "My Hello World!"'#10#10'myHello()');
	py.RunSimple('myHello()');
	//Import hello...
	// pName := PyString_FromString('hello');
	//   sysModule := PyImport_Import(pName);
	//   WriteLn(HexStr(sysModule));
	//   Py_DECREF(pName);

  // pFunc := PyObject_GetAttrString(sysModule, 'hello');  
  //   WriteLn('func: ', HexStr(pFunc));

  //pArgs := PyTuple_New(0);
  //PyObject_CallObject(pFunc, pArgs);

  //Py_DECREF(pArgs);
  //Py_DECREF(pFunc);

  // pPathObj := PyObject_GetAttrString(sysModule, 'path');
  //   WriteLn(HexStr(pPathObj));
  // 
  //   pFunc := PyObject_GetAttrString(pPathObj, 'insert');  
  //   WriteLn('func: ', HexStr(pFunc));
  //   
  //   pInt := PyInt_FromLong(0);
  //   WriteLn('int: ', HexStr(pInt));
  // 
  //   pStr := PyString_FromString(PChar('""'));
  //   WriteLn('pStr: ', HexStr(pStr));
  //     
  //   pArgs := PyTuple_New(2);
  //   PyTuple_SetItem(pArgs, 0, pInt);
  //   PyTuple_SetItem(pArgs, 0, pStr);
  //
  //PyObject_CallObject(pFunc, pArgs);
  //
  //Py_DECREF(pInt);
  //Py_DECREF(pStr);
  //Py_DECREF(pArgs);

	//PyRun_SimpleStringFlags('import hello', nil);
	//PyRun_SimpleStringFlags('print os.path.abspath(".");', nil);
	//PyRun_SimpleStringFlags('print sys.path', nil);
	//PyRun_SimpleStringFlags('hello.hello();', nil);	
	
	//WriteLn('Python unloaded');
end.